package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import connectUtil.ConnectionUtil;
import entity.Visitor;

public class SearchVisitor {
	private static Connection conn;
    private static CallableStatement callProc;
    private static ResultSet rs;
	public static ArrayList<Visitor> searchName(String name){
		ArrayList<Visitor> list = new ArrayList<Visitor>();
		Visitor e = null;
		try{
			conn = ConnectionUtil.getConnection();
    		callProc = conn.prepareCall("{call SearchName(?)}");
    		callProc.setString(1,name);
    		rs = callProc.executeQuery();
    		while(rs.next()){
    			e = new Visitor();
    			e.setFirstName(rs.getString(1));
    			e.setLastName(rs.getString(2));
    			e.setGender(rs.getString(3));
    			e.setTelephone(rs.getString(4));
    			e.setArea(rs.getString(5));
    			e.setHobby(rs.getString(6));
    			e.setDescription(rs.getString(7));
    			list.add(e);
    		}
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		finally{
			try {
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		System.out.println(" so luong visitor tim duoc: " + list.size());
		return list;
	}
}
